<?php

/**
 * Base project form.
 * 
 * @package    lezgro
 * @subpackage form
 * @author     Your name here 
 * @version    SVN: $Id: BaseForm.class.php 20147 2009-07-13 11:46:57Z FabianLange $
 */
class BaseForm extends sfFormSymfony
{
    public $hints = array();

    public function getHint($name) {
        return (isset($this->hints[$name]) ? $this->hints[$name] : false);
    }

    public function setHint($name, $value) {
        sfProjectConfiguration::getActive()->loadHelpers('I18N');
        $this->hints[$name] = $value;
        $this->widgetSchema[$name]->setAttribute('title', __($value));
    }

    public function setHints($hints) {
        $this->hints = array();
        foreach ($hints as $name => $value)
            $this->setHint($name, $value);
    }
    public function processErrorsForAjaxResponse(Array &$response) {
        $i18n = sfContext::getInstance()->getI18N();
        $errors = $this->getErrorSchema();
        if (!is_null($errors)) {
            foreach ($errors as $name => $error) {
                if (!empty($name)) {
                    $response['inputName'][$name] = $this[$name]->getParent()->getWidget()->generateName($this[$name]->getName());
                    $response['label'][$name] = $this[$name]->renderLabelName();
                } else {
                    $response['inputName'][$name] = '';
                    $response['label'][$name] = '';
                }
                if (count($error) > 1) {
                    foreach ($error as $err) {
                        $response['errors'][$name][] = $i18n->__($err->__toString());
                    }
                } else {
                    $response['errors'][$name] = $i18n->__($error->__toString());
                }
            }
            $response['errors_count'] = count($errors);
        }
        return $response;
    }
     public function bind(array $taintedValues = null, array $taintedFiles = null) {
        foreach ($taintedValues as $name => $value) {
            $hint = $this->getHint($name);

            if ($hint == $value)
                $taintedValues[$name] = '';
        }
        return parent::bind($taintedValues, $taintedFiles);
    }

}
